home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / telecomm / sticpsrc.lzh / SOURCE.ARC / ATARI_UT.C < prev    next >
C/C++ Source or Header  |  1990-05-11  |  6KB  |  319 lines

  1. /* Atari ST-specific functions, common to NET and BM */
  2. /* written by Rob Janssen, PE1CHL */
  3.  
  4. #include <stdio.h>
  5. #ifdef MWC
  6. #include <osbind.h>            /* os interface defines */
  7. #endif
  8.  
  9. #include "global.h"
  10. #include "atari.h"
  11.  
  12. /* name of tempfiles in program-dependent to allow concurrent running */
  13.  
  14. static char tmpfilename[2];
  15.  
  16. /* current drive and dir at startup */
  17.  
  18. int  st_curdrive;
  19. char st_curdir[66];
  20.  
  21. /* varnames for filename constants in files.c */
  22.  
  23. extern char startup[];
  24. extern char on_exit[];
  25. extern char userfile[];
  26. extern char hosts[];
  27. extern char fingerpath[];
  28. extern char mailspool[];
  29. extern char mailqdir[];
  30. extern char routeqdir[];
  31. extern char alias[];
  32. extern char tmpdir[],TMPDIR[];
  33. extern char bm_rc[];
  34.  
  35. void free();
  36. char *getenv(),*getnenv();
  37.  
  38. /* Atari ST specific initialization code */
  39.  
  40. atari_init(tmpfname)
  41.     char tmpfname;            /* 'b' or 'n' */
  42.  
  43. {
  44.     char dirname[64];        /* temp dirname buffer */
  45.  
  46.     tmpfilename[0] = tmpfname;    /* set temp file name char */
  47.  
  48.     st_curdrive = Dgetdrv();    /* get current drive (0,1,...) */
  49.  
  50.     if (Dgetpath(dirname,0) == 0){    /* get current dir */
  51.         sprintf(st_curdir,"%c:%s%s",(char) st_curdrive+'A',
  52.             (*dirname? "" : "\\"),dirname);
  53.     }
  54.  
  55.     /* fixup pathnames */
  56.  
  57.     startup[0] += st_curdrive;
  58.     on_exit[0] += st_curdrive;
  59.     userfile[0] += st_curdrive;
  60.     hosts[0] += st_curdrive;
  61.     fingerpath[0] += st_curdrive;
  62.     mailspool[0] += st_curdrive;
  63.     mailqdir[0] += st_curdrive;
  64.     routeqdir[0] += st_curdrive;
  65.     alias[0] += st_curdrive;
  66.     tmpdir[0] += st_curdrive;
  67.     bm_rc[0] += st_curdrive;
  68.  
  69.     printf("\033v\033e\033E\n");    /* autoWRAP ON, cursor ON, CLS, linefeed */
  70. #if 0
  71.     cleantemp();            /* clean the temp dir */
  72. #endif
  73. }
  74.  
  75. /* atari specific exit code */
  76.  
  77. atari_stop()
  78. {
  79. #if 0
  80.     cleantemp();            /* remove any created temp files */
  81. #endif
  82.     Dsetdrv(st_curdrive);        /* reset current drive */
  83.     Dsetpath(st_curdir);        /* reset directory */
  84. }
  85.  
  86. #if 0
  87. /* remove temp files (tempdir\[BN]*.TMP) */
  88. cleantemp ()
  89.  
  90. {
  91.     char filename[80];
  92.     struct dta dta;
  93.  
  94.     Fsetdta(&dta);            /* set transfer area */
  95.  
  96.     strcpy(filename,getnenv(TMPDIR));
  97.     strcat(filename,"\\");
  98.     strcat(filename,tmpfilename);    /* "b" or "n" */
  99.     strcat(filename,"*.TMP");
  100.  
  101.     if (Fsfirst(filename,~0x18) == 0)
  102.         do
  103.         {
  104.             strcpy(filename,getnenv(TMPDIR));
  105.             strcat(filename,"\\");
  106.             strcat(filename,dta.fname);
  107.             unlink(filename);
  108.         } while (Fsnext() == 0);
  109. }
  110. #endif
  111.  
  112. /* build temp file name
  113.  * this version is used instead of the lib routine to be able to set
  114.  * tmp directory name, and to provide more unique names
  115.  */
  116.  
  117. char *tmpnam (s)
  118.     char *s;
  119.  
  120. {
  121.     static char buf[64];
  122.     static long sequence = 0;
  123.  
  124.     if (s == NULLCHAR)
  125.         s = buf;
  126.  
  127.     sprintf(s,"%s\\%s%07ld.TMP",getnenv(TMPDIR),tmpfilename,sequence++);
  128.  
  129.     return (s);
  130. }
  131.  
  132. /* create and open temp file */
  133. /* name is administered and it will be deleted by our replacement fclose() */
  134.  
  135. struct tempfiles {
  136.     struct tempfiles *next;
  137.     struct tempfiles *prev;
  138.     FILE *fp;
  139.     char name[1];
  140. } *tempfiles = NULL;
  141.  
  142. FILE *tmpfile ()
  143.  
  144. {
  145.     char *name;
  146.     FILE *fp;
  147.     register struct tempfiles *tf;
  148.  
  149.     name = tmpnam(NULLCHAR);
  150.  
  151.     if ((tf = (struct tempfiles *) calloc(1,sizeof(struct tempfiles) + strlen(name))) == NULL)
  152.         return NULL;
  153.  
  154.     if ((fp = fopen(name,"w+")) == NULLFILE)
  155.         free(tf);
  156.     else {
  157.         if ((tf->next = tempfiles) != NULL)
  158.             tempfiles->prev = tf;
  159.  
  160.         tempfiles = tf;
  161.         tf->fp = fp;
  162.         strcpy(tf->name,name);
  163.     }
  164.  
  165.     return (fp);
  166. }
  167.  
  168. /* replacement for fclose().  checks if it is a tempfile and deletes it. */
  169.  
  170. #undef fclose
  171. int notmpfclose (fp)
  172. FILE *fp;
  173.  
  174. {
  175.     int rv;
  176.     register struct tempfiles *tf;
  177.  
  178.     rv = fclose(fp);        /* first close the file */
  179.  
  180.     for (tf = tempfiles; tf != NULL; tf = tf->next)
  181.         if (tf->fp == fp) {    /* this one a tempfile? */
  182.             unlink(tf->name); /* then remove it */
  183.  
  184.             if (tf->next != NULL)
  185.                 tf->next->prev = tf->prev;
  186.  
  187.             if (tf->prev != NULL)
  188.                 tf->prev->next = tf->next;
  189.             else
  190.                 tempfiles = tf->next;
  191.  
  192.             free((char *) tf);
  193.             break;
  194.         }
  195.  
  196.     return rv;            /* return fclose() result */
  197. }
  198.  
  199. #ifdef MWC
  200. /* rename a file (MW C doesn't have it) */
  201.  
  202. int rename (s,d)
  203.     char *s;            /* source name */
  204.     char *d;            /* dest name */
  205.  
  206. {
  207.     return (Frename(0,s,d) != 0L);    /* call gemdos function */
  208. }
  209. #endif
  210.  
  211. #ifdef __TURBOC__
  212. /* create and remove directory (Turbo C doesn't have it) */
  213.  
  214. int mkdir (name)
  215.     char *name;
  216.  
  217. {
  218.     return (Dcreate(name) != 0L);    /* call gemdos function */
  219. }
  220.  
  221. int rmdir (name)
  222.     char *name;
  223.  
  224. {
  225.     return (Ddelete(name) != 0L);    /* call gemdos function */
  226. }
  227.  
  228. /* test accessability of a file (Turbo C doesn't have it) */
  229.  
  230. int access (name,mode)
  231.     char *name;
  232.     int mode;
  233.  
  234. {
  235.     int rcode;
  236.     struct dta dta,*orgdta;
  237.  
  238.     orgdta = Fgetdta();        /* save caller's DTA */
  239.     Fsetdta(&dta);            /* set our's */
  240.     rcode = Fsfirst(name,~0x08);    /* get all but volume lael */
  241.     Fsetdta(orgdta);        /* restore DTA */
  242.  
  243.     if (rcode == 0){        /* found the file? */
  244.         if (mode & 1)        /* exec */
  245.             if (dta.attr & 0x18)
  246.                 return 1;
  247.  
  248.         if (mode & 2)        /* write */
  249.             if (dta.attr & 0x19)
  250.                 return 1;
  251.  
  252.         if (mode & 4)        /* read */
  253.             if (dta.attr & 0x08)
  254.                 return 1;
  255.  
  256.         return 0;
  257.     }
  258.  
  259.     return 1;
  260. }
  261. #endif
  262.  
  263. #ifdef MWC
  264. /* special version of calloc() that runs sligthly faster */
  265. /* depends on Mark Williams routine lmalloc() */
  266.  
  267. char *calloc (count,size)
  268.     unsigned count,size;
  269.  
  270. {
  271.     register unsigned long n;
  272.     register int16 *p;
  273.     char *rv,*lmalloc();
  274.  
  275.     /* most calls specify a count of 1, save a long multiply.... */
  276.     n = (count == 1)? size : (unsigned long) count * size;
  277.  
  278.     if ((rv = lmalloc(n)) != NULLCHAR)
  279.     {
  280.         if (n = (n + 1) / 2){    /* compute number of words */
  281.         p = (int16 *) rv;
  282.         do
  283.         {
  284.             *p++ = 0;
  285.         } while (--n);
  286.         }
  287.     }
  288.  
  289.     return rv;
  290. }
  291.  
  292. /* a free() routine with some extra checks */
  293. /* note that this will introduce a "free_ redefined" warning from */
  294. /* the linker, but this can be ignored.     define QFREE to skip this */
  295. /* version, and use a macro (in global.h) instead. */
  296.  
  297. # ifndef QFREE
  298. void
  299. free (p)
  300. register unsigned long *p;
  301. {
  302.     unsigned long s;
  303.     extern unsigned long *_a_scanp;
  304.  
  305.     if ((s = p[-1]) == 0 || s > 1048576L || notmem(p)){
  306.         printf("WARNING!! freeing garbage (%ld @ 0x%lx)\n",s,p);
  307.         return;
  308.     }
  309.  
  310.     if (s & 1){
  311.         printf("WARNING!! same area freed twice (%ld @ 0x%lx)\n",s & ~1L,p);
  312.         return;
  313.     }
  314.  
  315.     *(_a_scanp = p - 1) |= 1;
  316. }
  317. # endif
  318. #endif
  319.